Data filtering

Author

Stan Brouwer

This is the code used to identify and cut out each of the lifts from the IMU’s data. This process has been repeated for each subject, and the resulting data is stored in separate csv files for further analysis. The process is described below

Lets load all functions from the previous chapter. This is a hassle since they are stored in Quarto markdown language, and R only accept real R code.

Code
# Clean workspace
rm(list = ls())
library(tidyr)
library(dplyr)
library(plotly)
library(ggplot2)
library(knitr)
library(lubridate)

# Most dependencies are loaded by loading datafiltering.qmd.
# To load only the chuncks containing functions we need parsermd
library(parsermd)

toload <- c("load_data","load_plots", "filter_data", "separate_lifts", "visualise_seperate_lifts")
rmd <- parse_rmd("datafiltering.qmd")


for (i in seq_along(toload)) {
  setup_chunk <- rmd_select(rmd, toload[i]) |> 
    as_document()

  setup_chunk <- setup_chunk[-grep("```", setup_chunk)]
  setup_chunk
#> [1] "library(tidyr)"   "library(stringr)" ""                

  eval(parse(text = setup_chunk))             
}
rm(rmd, i, setup_chunk, toload)

I’ve collected metadata about all recorded lift in liftinfo.csv. This can be used to easialy load the lifts:

Code
metadata <- read.csv("../../Logs/metadata.csv", header = TRUE, sep = ";")
Code
LoadXsenseData2 <- function(dir) {
  hz <- 60
  skiprow <- 7


  files <- list.files(path = dir, full.names = TRUE)
  data <- list()

  # Read CSV files of each directory
  for (i in seq_along(files)) {
    data[[i]] <- read.csv(files[i], header = TRUE, skip = skiprow)
  }

  # Ensure all dataframes have the same number of rows
  min_rows <- min(sapply(data, nrow))
  data <- lapply(data, function(df) {
    df <- df[1:min_rows, , drop = FALSE]
    return(df)
  })

  # Adjust time
  for (i in seq_along(data)) {
    rows <- nrow(data[[i]])
    data[[i]]$TimeS <- ((1/hz) * (1:rows))
  }

  # Initialize toreturn data frame with time column
  toreturn <- data.frame(time = data[[1]]$TimeS)

  # Calculate absolute values
  for (i in 1:length(data)) {
    if ("FreeAcc_X" %in% names(data[[i]])) {
      col_name <- paste0("FreeAcc_abs", i)
      toreturn[[col_name]] <- sqrt(data[[i]]$FreeAcc_X^2 + data[[i]]$FreeAcc_Y^2 + data[[i]]$FreeAcc_Z^2)
    }
    if ("Acc_X" %in% names(data[[i]])) {
      col_name <- paste0("A_abs", i)
      toreturn[[col_name]] <- sqrt(data[[i]]$Acc_X^2 + data[[i]]$Acc_Y^2 + data[[i]]$Acc_Z^2)
    }
    if ("Gyr_X" %in% names(data[[i]])) {
      col_name <- paste0("Gyr_abs", i)
      toreturn[[col_name]] <- sqrt(data[[i]]$Gyr_X^2 + data[[i]]$Gyr_Y^2 + data[[i]]$Gyr_Z^2)
    }
  }

  # Order the attributes of the dataframe
  toreturn_sorted <- toreturn[, sort(names(toreturn))]

  return(toreturn_sorted)
}

Here is the code to cut each lift and store it as a R object

Code
dir = "../../Logs/21mei2"
files <- list.files(path = dir, full.names = TRUE)

data <- LoadXsenseData2(files[6])
plot_a(data)
Code
#data1 <- filterdata(data)
#data1 <- separatelifts(data1)

#i = 1
#plot_filtered_subplots(data1)
Code
#cat("lift ", i)
#proefpersoon <- metadata$naam[x]
#cat("start time: ")
#start_time <- readline()
#cat("end time: ")
#end_time <- readline()
#df <- data1[[i]]
  
#time_column <- "time"
#output_filename <- paste0(proefpersoon, i)
  
# Filter the dataframe
#filtered_df <- df[df[[time_column]] >= start_time & df[[time_column]] <= end_time, ]
  
# Save the filtered dataframe to a CSV file
#write.csv(filtered_df, output_filename, row.names = FALSE)

#i <- i + 1